/* $RCSFile: ADNDynCustomerRelationShip.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynCustomerRelationShip.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:32  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using CrmSdk;

namespace AddonNice.CRMWrapper
{
	public class ADNDynCustomerRelationShip : DynEntity
	{
        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.customerrelationship)
            {}

            public override Type DynType
            { get { return typeof(ADNDynCustomerRelationShip); } }

            public override string DynTypename
            {   get{ return CrmStr.customerrelationship;}}

            public override DynEntity Build()
            {   return new ADNDynCustomerRelationShip();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynCustomerRelationShip(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynCustomerRelationShip(w);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynCustomerRelationShip(w,PrimaryKeyValue);}
        }

        static ADNDynCustomerRelationShip(){new Ftor();} 

        public override bool HasPrimaryField
        {
            get{   return false;}
        }

        public override bool HasOwningBusinessUnitProperty
        {
            get{   return true;}
        }

        /// <summary>
		/// Reserved for template methods: do not use directly
		/// </summary>
        public ADNDynCustomerRelationShip(): base()
		{}

        public ADNDynCustomerRelationShip(CRMWrapperBase w)
			: base(w,CrmStr.customerrelationship)
		{}

		public ADNDynCustomerRelationShip(CRMWrapperBase w, Guid CrmId)
			: base(w,CrmStr.customerrelationship, CrmId)
		{}

		public ADNDynCustomerRelationShip(CRMWrapperBase w, InternalDynamicEntity e)
			: base(w,CrmStr.customerrelationship,e,null,null)
		{}

		public ADNDynCustomerRelationShip(CRMWrapperBase w, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
           : base(w,CrmStr.customerrelationship,e,prop,s)
		{}

        public ADNDynCustomerRelationShip(ResetWrapperHandler fn)
			: base(fn,CrmStr.customerrelationship)
		{}

		public ADNDynCustomerRelationShip(ResetWrapperHandler fn, Guid CrmId)
			: base(fn,CrmStr.customerrelationship, CrmId)
		{}

		public ADNDynCustomerRelationShip(ResetWrapperHandler fn, InternalDynamicEntity e)
			: base(fn,CrmStr.customerrelationship,e,null,null)
		{}

		public ADNDynCustomerRelationShip(ResetWrapperHandler fn, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
           : base(fn,CrmStr.customerrelationship,e,prop,s)
		{}

        [CrmDynAttribute(CrmStr.customerrelationship)]
        public Guid customerrelationshipid
		{
			get
			{
				return KeyPropertyValue(CrmStr.customerrelationshipid);
			}
			set
			{
				AddKeyProperty(CrmStr.customerrelationshipid, value);
			}
		}
        
        protected override void SetCrmIdProperty(Guid crmId)
        {
           if ( !dicProperty.ContainsKey(CrmStr.customerrelationshipid) || KeyPropertyValue(CrmStr.customerrelationshipid) != crmId )
                customerrelationshipid  =   crmId;
        }

        public override Guid PrimaryKeyGuidValue
        {
           get
           {
                return customerrelationshipid;
           }
           set
           {
               customerrelationshipid    =   value;
           }
        }

        [CrmDynAttribute(CrmStr.customerrelationship)]
        public string customerroledescription
        {
            get
            {
                return StringPropertyValue(CrmStr.customerroledescription);
            }
            set
            {
                AddStringProperty(CrmStr.customerroledescription,value);
            }
        }
        
        #region customer

        string _DefaultCustomerName   =   CrmStr.account;
		public string DefaultCustomerName
		{
			get
			{
				return _DefaultCustomerName;
			}
			set
			{
                switch (value)
                {
                    case CrmStr.account:
                    case CrmStr.contact:
                        _DefaultCustomerName  =   value;
                        break;
                }
			}
		}

        /// <summary>
        /// Beware : set defaults to account, to change it set DefaultCustomerName property before calling set
        /// or use SetContactCustomerid for adding contacts
        /// </summary>
        [CrmDynAttribute(CrmStr.customerrelationship)]
        public Guid customerid 
        {
            get 
            {
                return CustomerPropertyValue(CrmStr.customerid);
            }
            set 
            {
                AddCustomerProperty(CrmStr.customerid,DefaultCustomerName,value);
            }
        }

        /// <summary>
        /// The entity : account or contact
        /// </summary>
        public string customerType
        {
            get 
            {
                return CustomerPropertyType(CrmStr.customerid);
            }
        }

         /// <summary>
        /// To adding contact customerids
        /// </summary>
        public void SetContactCustomerid(Guid contactid)
        {
                AddCustomerProperty(CrmStr.customerid,CrmStr.contact,contactid);
        }

        #endregion customer

        /// <summary>
        ///  Gets or sets the ID of the converse relationship of the customer relationship. 
        /// </summary>
        [CrmDynAttribute(CrmStr.customerrelationship)]
        public Guid converserelationshipid
        {
			get
			{
				return LookupPropertyValue(CrmStr.converserelationshipid);
			}
			set
			{
				AddLookupProperty(CrmStr.converserelationshipid,CrmStr.customerrelationship, value);
			}
        }
 
        /// <summary>
        ///  Gets or sets the ID of the customer role. 
        /// </summary>
        [CrmDynAttribute(CrmStr.customerrelationship)]
        public Guid customerroleid
        {
			get
			{
				return LookupPropertyValue(CrmStr.customerroleid);
			}
			set
			{
				AddLookupProperty(CrmStr.customerroleid,CrmStr.relationshiprole, value);
			}
        }
        
        /// <summary>
        ///  Gets or sets the ID of the secondary customer in the relationship. 
        /// </summary>
        [CrmDynAttribute(CrmStr.customerrelationship)]
        public Guid partnerid
        {
			get
			{
                return CustomerPropertyValue(CrmStr.partnerid);
            }
            set 
            {
                AddCustomerProperty(CrmStr.partnerid,CrmStr.account,value);
			}
        }
        
        /// <summary>
        ///  Gets or sets the ID of the secondary customer in the relationship. 
        /// </summary>
        public string partneridName
        {
			get
			{
                return CustomerPropertyName(CrmStr.partnerid);
            }
        }

        /// <summary>
        /// To adding contact customerids
        /// </summary>
        public void SetContactPartnerid(Guid contactid)
        {
                AddCustomerProperty(CrmStr.partnerid,CrmStr.contact,contactid);
        }

        /// <summary>
        /// Contains the description of the customer and the customer's role in this relationship. 
        /// </summary>
        [CrmDynAttribute(CrmStr.customerrelationship)]
        public string partnerroledescription
        {
            get
            {
                return StringPropertyValue(CrmStr.partnerroledescription);
            }
            set
            {
                AddStringProperty(CrmStr.partnerroledescription,value);
            }
        }

        /// <summary>
        ///  Gets or sets the ID of the relationship role of the secondary customer. 
        /// </summary>
        [CrmDynAttribute(CrmStr.customerrelationship)]
        public Guid partnerroleid
        {
			get
			{
				return LookupPropertyValue(CrmStr.partnerroleid);
			}
			set
			{
				AddLookupProperty(CrmStr.partnerroleid,CrmStr.relationshiprole, value);
			}
        }

        /// <summary>
        /// virtual because on 'detail' records it is only a uniqueidentifier property
        /// </summary>
        [CrmDynAttribute(CrmStr.customerrelationship)]
        public Guid owningbusinessunit
        {
            get
            {
                return LookupPropertyValue(CrmStr.owningbusinessunit);
            }
            set
            {
                AddLookupProperty(CrmStr.owningbusinessunit,CrmStr.businessunit,value);
            }
        }

        public string owningbusinessunitName
        {
            get
            {
                return LookupPropertyName(CrmStr.owningbusinessunit);
            }
        }

        /// <summary>
        /// Owner for this object
        /// </summary>
        [CrmDynAttribute(CrmStr.customerrelationship)]
        public override Guid ownerid
        {
            get
            {
                if ( ContainsKey(CrmStr.ownerid) )
                    return OwnerPropertyValue(CrmStr.ownerid);
                return Guid.Empty;
            }
            set
            {
                AddOwnerProperty(CrmStr.ownerid,CrmStr.systemuser,value);
            }
        }
	}
}
